<?php
// -----------------------------------------------------------------------------
// 引入
// -----------------------------------------------------------------------------

include "../config/config.php";
include "../auth.php";
include "../DBManager.php";
include "../resources/Snowflake/HighConcurrencySnowflake.php";

// -----------------------------------------------------------------------------
// 访问设置
// -----------------------------------------------------------------------------

if (!isset($_POST["linkTitle"]) || !isset($_POST["linkUrl"]) || !isset($_POST["categoryId"]) || !isset($_POST["linkDescription"]) || !isset($_POST["targetSortNumber"])) {
    exit("需要传入有效的参数！");
}

// -----------------------------------------------------------------------------
// 数据库设置
// -----------------------------------------------------------------------------

$dbPath = "../" . $dbPath;
$db = new DBManager("sqlite:" . $dbPath);

// -----------------------------------------------------------------------------
// 时区设置
// -----------------------------------------------------------------------------

date_default_timezone_set($region);

// -----------------------------------------------------------------------------

/* 使用雪花算法生成排序号 */
$snowflake = new HighConcurrencySnowflake();
$sortNumber = $snowflake->nextId();

// -----------------------------------------------------------------------------

$timeCreate = (string)time();
$title = $_POST["linkTitle"];
$url = $_POST["linkUrl"];
$categoryId = $_POST["categoryId"];
$description = $_POST["linkDescription"];
$targetSortNumber = $_POST["targetSortNumber"];

$timesOfClick = 0;

// -----------------------------------------------------------------------------

// if ($db->checkIfCategoryExistsByTitle($title)) {
//     exit("创建失败，因为该 category 名称已经存在。");
// }

// -----------------------------------------------------------------------------

$arr = compact("title", "url", "categoryId", "description", "sortNumber", "timeCreate", "timesOfClick", "targetSortNumber");

$db->insertIntoLinks($arr);
echo "链接创建成功！";
